package com.attms.service.impl;

import com.attms.dao.AbsentDao;
import com.attms.dao.EmployeeDao;
import com.attms.domain.Absent;
import com.attms.domain.Employee;
import com.attms.service.IGManagerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.List;

import static java.time.temporal.ChronoUnit.DAYS;
@Service
public class GmanagerServiceImpl extends ServiceImpl<AbsentDao, Absent> implements IGManagerService {
    @Autowired
    private AbsentDao absentDao;
    @Autowired
    private EmployeeDao employeeDao;
    @Override
    public boolean solveLeave(Integer isAgree, Absent absenter) {
//首先，现在的状态一定是经理审批过了（或者人事审核过了），且请假时间超过了三天
        LambdaQueryWrapper<Absent> Awrapper = new LambdaQueryWrapper<>();
        Awrapper.eq(Absent::getA_ID,absenter.getA_ID());
        Absent absence = absentDao.selectOne(Awrapper);

//        如果总经理同意，则3申请通过，否则4申请失败

        if(isAgree==0){
            absence.setA_RESPON(absenter.getA_REASON());
            absence.setA_STATE(4);
        }else{
            absence.setA_RESPON("总经理同意批准假期");
            absence.setA_STATE(2);
            int days = absence.getA_ENDTIME().until(absence.getA_STTIME()).getDays();
            //如果是因公请假，则不扣薪，只记录
            //如果是因公请假，则不扣薪，只记录
//            Employee nowperson = employeeDao.selectById(absence.getE_ID());
//            if(absence.getA_TYPE()==8){//如果事假，则记录扣薪时长
//                int beforeAbs = nowperson.getE_ABSHOUR();
//                System.out.println(beforeAbs + days * 8);
//                nowperson.setE_ABSHOUR(beforeAbs + days * 8);
//                employeeDao.updateById(nowperson);
//            }else if(absence.getA_TYPE() > 0){//如果是带薪假期，则只修改请的假的剩余天数
//                int vocationType = absence.getA_TYPE();
//                if(vocationType == 1)nowperson.setE_RYEAR(nowperson.getE_RYEAR() - days);
//                else if(vocationType == 2)nowperson.setE_RILL(nowperson.getE_RILL() - days);
//                else if(vocationType == 3)nowperson.setE_RWEDDING(nowperson.getE_RWEDDING() - days);
//                else if(vocationType == 5)nowperson.setE_RMATERNITY(nowperson.getE_RMATERNITY() - days);
//                else if(vocationType == 6)nowperson.setE_RLACTATION(nowperson.getE_RLACTATION() - (int)Duration.between(absence.getA_STTIME(), absence.getA_ENDTIME()).toHours());
//                else if(vocationType == 7)nowperson.setE_RPATERNITY(nowperson.getE_RPATERNITY() - days);
//                employeeDao.updateById(nowperson);
//            }
        }
        absentDao.updateById(absence);
        return true;
    }

    @Override
    public List<Absent> getPersonalAbsentList(Absent absenter) {
        LambdaQueryWrapper<Absent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Absent::getE_ID, absenter.getE_ID())
                .ge(Absent::getA_STTIME, absenter.getA_STTIME())
                .le(Absent::getA_ENDTIME, absenter.getA_ENDTIME());
        return absentDao.selectList(wrapper);
    }

    @Override
    public List<Absent> getAllAbsentList(Absent absent) {
        LambdaQueryWrapper<Absent> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(Absent::getA_STTIME, absent.getA_STTIME())
                .le(Absent::getA_ENDTIME, absent.getA_ENDTIME());
        return absentDao.selectList(wrapper);
    }
}
